const { app } = require('./app');
const { querySql } = require('./mysql')
    // const { querySql } = require('./mysql');
const resultData = {
    code: '',
    msg: '',
    data: []
};
//只关注40行以下的代码
app.post('/query/book', (req, res) => {
    console.log(req.body);
    var sql = 'select * from books';
    var sqlParam = [];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            resultData.code = '5000'
            resultData.msg = "查询失败，请联系客服";
            res.send(resultData);
        } else {
            resultData.code = '2000'
            resultData.msg = '查询成功';
            resultData.data = result
            res.send(resultData);
        }
    })
})
app.post('/book/add', (req, res) => {
    console.log(req.body);
    var sql = 'insert into books(name,author,price) values(?,?,?)';
    var sqlParam = [req.body.name, req.body.author, req.body.price];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            resultData.code = '5000';
            resultData.msg = '添加失败'
            res.send(resultData);
        } else {
            resultData.code = "2000";
            resultData.msg = '添加成功'
            res.send(resultData);
        }
    })
})
app.get('/book/del', (req, res) => {
    console.log(req.query);
    var sql = 'delete from books where id = ?';
    var sqlParam = [req.query.id];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            resultData.code = '5000';
            resultData.msg = '删除失败'
            res.send(resultData);
        } else {
            resultData.code = '2000';
            resultData.msg = '删除成功'
            res.send(resultData);
        }
    })
})
app.post('/book/update', (req, res) => {
    var sql = 'update books set name = ?,author = ?, price = ? where id = ?';
    var sqlParam = [req.body.name, req.body.author, req.body.price, req.body.id];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            res.send(err);
        } else {
            res.send(result);
        }
    })
})

app.post('/book/getById', (req, res) => {
    console.log(req.body);
    var sql = 'select * from books where id = ?';
    var sqlParam = [req.body.id];
    querySql(sql, sqlParam, (err, result) => {
        if (err) {
            res.send(err);
        } else {
            res.send(result);
        }
    })
})